﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace Surveillance
{
    public class Player 
    {
        #region Declerations
        private const float CAMERA_SPEED = 1.0f;
        private const float ZOOM_SPEED = 0.01f;

        private Texture2D dotTexture;
        private SpriteFont font;
        private Rectangle targetRectangle;
        private Vector2 worldPosition;
        private MouseState oldMouseState;
        private GraphicsDevice graphicsDevice;
        private bool spaceDown;

        #endregion

        #region Constructors

        public Player(ContentManager content, GraphicsDevice graphics, GameWindow window)
        {
            dotTexture = content.Load<Texture2D>(@"Textures\dot");
            font = content.Load<SpriteFont>(@"Textures\SpriteFont1");
            graphicsDevice = graphics;
            worldPosition = new Vector2(Game1.cam.Pos.X + graphics.Viewport.Width, Game1.cam.Pos.Y + graphics.Viewport.Height);
            targetRectangle = new Rectangle((int)worldPosition.X, (int)worldPosition.Y, 10, 10);
            Mouse.WindowHandle = window.Handle;
            oldMouseState = Mouse.GetState();
            spaceDown = false;
        }

        #endregion

        #region Update and Draw

        public void Update(GameTime gameTime, List<Switch> switchList, List<Keypad> padList)
        {

            float elapsed = gameTime.ElapsedGameTime.Milliseconds;
            KeyboardState keyboardState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();
            worldPosition = new Vector2(Game1.cam.Pos.X + graphicsDevice.Viewport.Width, Game1.cam.Pos.Y + graphicsDevice.Viewport.Height);
            Vector2 worldLocation = Game1.cam.screenToWorld(Game1.cam.Pos);
            bool clicked = ((oldMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) || 
                            (spaceDown && keyboardState.IsKeyUp(Keys.Space)) && !Game1.activeTextBox);
            if ((keyboardState.IsKeyDown(Keys.Up) || mouseState.Y <= 10 && mouseState.Y > -1) && (Game1.cam.Pos.Y > -100))
            {
                Game1.cam.Move(new Vector2(0.0f,-1 * CAMERA_SPEED * elapsed));
            }
            else if ((keyboardState.IsKeyDown(Keys.Down) || mouseState.Y >= graphicsDevice.Viewport.Height - 10 && mouseState.Y < graphicsDevice.Viewport.Height + 1) &&
                (Game1.cam.Pos.Y < Game1.WORLD_HEIGHT - graphicsDevice.Viewport.Height * 2 + 100))

            {
                Game1.cam.Move(new Vector2(0.0f, CAMERA_SPEED * elapsed));
            }
            else if ((keyboardState.IsKeyDown(Keys.Left) || mouseState.X <= 10 && mouseState.X > -1) && (Game1.cam.Pos.X > -100) )
            {
                Game1.cam.Move(new Vector2(-1 * CAMERA_SPEED * elapsed, 0.0f));
            }
            else if ((keyboardState.IsKeyDown(Keys.Right) || mouseState.X >= graphicsDevice.Viewport.Width - 10 && mouseState.X < graphicsDevice.Viewport.Width + 1) && 
                    (Game1.cam.Pos.X < Game1.WORLD_WIDTH - graphicsDevice.Viewport.Width*2 + 100))
            {
                Game1.cam.Move(new Vector2(CAMERA_SPEED * elapsed, 0.0f));
            }
            if (mouseState.ScrollWheelValue - oldMouseState.ScrollWheelValue > 0)
            {
                if (Game1.currentZoomLevel < 1)
                {
                    Game1.cam.Move(new Vector2(graphicsDevice.Viewport.Width , graphicsDevice.Viewport.Height));
                    Game1.currentZoomLevel++;
                    Game1.cam.Zoom = Game1.ZOOM_LEVEL[Game1.currentZoomLevel];
                }
            }
            if (keyboardState.IsKeyDown(Keys.Space) && !Game1.activeTextBox)
            {
                spaceDown = true;
            }
            else if (mouseState.ScrollWheelValue - oldMouseState.ScrollWheelValue < 0)
            {
                if (Game1.currentZoomLevel > 0)
                {
                    Game1.cam.Move(new Vector2(-1 * (graphicsDevice.Viewport.Width), -1 * (graphicsDevice.Viewport.Height)));
                    Game1.currentZoomLevel--;
                    Game1.cam.Zoom = Game1.ZOOM_LEVEL[Game1.currentZoomLevel];
                }
            }
            onSwitch(switchList, clicked);
            onKeypad(padList, clicked);
            if (clicked) { spaceDown = false; }
            oldMouseState = mouseState;
            worldPosition.X = Game1.cam.Pos.X + graphicsDevice.Viewport.Width;
            worldPosition.Y = Game1.cam.Pos.Y + graphicsDevice.Viewport.Height;
            targetRectangle.X = (int)worldPosition.X;
            targetRectangle.Y = (int)worldPosition.Y;
        }

        private void onSwitch(List<Switch> switchList, bool clicked)
        {
            Rectangle screenPos;
            foreach (Switch swtch in switchList)
            {
                screenPos = Game1.cam.WorldToScreen(swtch.TargetRectangle);
                screenPos.Inflate(200, 200);
                screenPos.X += swtch.BiasX;
                screenPos.Y += swtch.BiasY;
                // check that cursor is within 20 px from the center of the switch
                if (  targetRectangle.Intersects(screenPos))
                {
                    swtch.mark();
                    if (clicked)
                    {
                        swtch.click();
                    }
                }
                else
                {
                    swtch.unmark();
                }
            }
        }

        private void onKeypad(List<Keypad> padList, bool clicked)
        {
            Rectangle screenPos;
            foreach (Keypad pad in padList)
            {
                screenPos = Game1.cam.WorldToScreen(pad.TargetLocation);
                screenPos.Inflate(200, 200);
                screenPos.X += 750;
                screenPos.Y += 650;
                // check that cursor is within 20 px from the center of the keypad
                if (targetRectangle.Intersects(screenPos))
                {
                    pad.mark();
                    if (clicked)
                    {

                        pad.click();
                    }
                }
                else
                {
                    pad.unmark();
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {

            spriteBatch.Draw
                (dotTexture,
                targetRectangle,
                Color.White);
        }

        #endregion

        
    }
}
